package com.cn.threadpool;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * FUN: 线程池类 功能说明： 是线程池的总控构建类, 实现通过根据可传入参数构造相应的执行队列、线程工厂、线程池执行器
 *
 * @author sushy
 * @date 2016-3-23 
 */
public class MyThreadPool {

	public static String DEFAULT_REJECTED_ERRMSG = "系统繁忙，请稍后再试!";
	public static String DEFAULT_PAUSED_ERRMSG = "暂停服务，请稍后再试!";
	private final static AtomicInteger threadPoolNumber = new AtomicInteger(1);
	private String poolName;// 线程池名称
	private String poolDesc;// 线程池描述
	private int maxQueueLength;// 队列最大长度
	private long timeOut;// 应用超时时间(单位秒)
	private BlockingQueue<Runnable> workQueue;// 工作队列
	private MyThreadFactory threadFactory;// 线程加工厂
	private MyRejectedExecutionHandler handler;// 拒绝请求处理器
	private MyThreadPoolExecutor executor;// 线程池执行器

	public MyThreadPool(int corePoolSize, int maximumPoolSize,
			int maxQueueLength, int keepAliveTime, long timeOut,
			boolean isCallerRuns) {
		this("sys",
				"pool" + String.valueOf(threadPoolNumber.getAndIncrement()),
				corePoolSize, maximumPoolSize, maxQueueLength, keepAliveTime,
				timeOut, isCallerRuns);
	}

	public MyThreadPool(String poolName, int corePoolSize,
			int maximumPoolSize, int maxQueueLength, int keepAliveTime,
			long timeOut, boolean isCallerRuns) {
		this("crm", poolName, corePoolSize, maximumPoolSize, maxQueueLength,
				keepAliveTime, timeOut, isCallerRuns);
	}

	private MyThreadPool(String poolNamePrefix, String poolName,
			int corePoolSize, int maximumPoolSize, int maxQueueLength,
			int keepAliveTime, long timeOut, boolean isCallerRuns) {
		this.poolName = poolName;
		this.maxQueueLength = maxQueueLength;
		this.timeOut = timeOut;

		// 构造初始化队列
		if (maxQueueLength > 1) {
			// 采用数组队列
			this.workQueue = new LinkedBlockingQueue<Runnable>(maxQueueLength);
		} else {
			// 不需要队列缓存
			this.workQueue = new SynchronousQueue<Runnable>();
		}

		// 初始化线程工厂类
		this.threadFactory = new MyThreadFactory(poolNamePrefix, poolName);

		// 拒绝请求处理器
		this.handler = new MyRejectedExecutionHandler(isCallerRuns,
				DEFAULT_REJECTED_ERRMSG);

		// 线程池执行器
		this.executor = new MyThreadPoolExecutor(Math.max(corePoolSize, 1),
				maximumPoolSize, keepAliveTime, this.workQueue,
				this.threadFactory, this.handler);
	}

	public String getPoolName() {
		return poolName;
	}

	public String getPoolDesc() {
		return poolDesc;
	}

	public void setPoolDesc(String poolDesc) {
		this.poolDesc = poolDesc;
	}

	public int getCorePoolSize() {
		return this.executor.getCorePoolSize();// 内核实例数
	}

	public void setCorePoolSize(int corePoolSize) {
		this.executor.setCorePoolSize(corePoolSize);
	}

	public int getMaximumPoolSize() {
		return this.executor.getMaximumPoolSize();// 最大线程数
	}

	public void setMaximumPoolSize(int maximumPoolSize) {
		this.executor.setMaximumPoolSize(maximumPoolSize);
	}

	public long getKeepAliveTime() {
		return this.executor.getKeepAliveTime(TimeUnit.SECONDS);// 最大空闲时间
	}

	public void setKeepAliveTime(long keepAliveTime) {
		this.executor.setKeepAliveTime(keepAliveTime, TimeUnit.SECONDS);
	}

	public int getMaxQueueLength() {
		return maxQueueLength;
	}

	public void setMaxQueueLength(int maxQueueLength) {
		this.maxQueueLength = maxQueueLength;
	}

	public long getTimeOut() {
		return timeOut;
	}

	public void setTimeOut(long timeOut) {
		this.timeOut = timeOut;
	}

	public long getRejectedTimes() {
		return this.handler.getRejectedTimes();// 拒绝请求次数
	}

	public String getRejectedErrMsg() {
		return this.handler.getRejectedErrMsg(); // 拒绝请求反馈的错误信息
	}

	public void setRejectedErrMsg(String rejectedErrMsg) {
		this.handler.setRejectedErrMsg(rejectedErrMsg);
	}

	public BlockingQueue<Runnable> getWorkQueue() {
		return this.workQueue;
	}

	public void setWorkQueue(BlockingQueue<Runnable> workQueue) {
		this.workQueue = workQueue;
	}

	public MyThreadFactory getThreadFactory() {
		return this.threadFactory;
	}

	public void setThreadFactory(MyThreadFactory threadFactory) {
		this.threadFactory = threadFactory;
	}

	public RejectedExecutionHandler getHandler() {
		return this.handler;
	}

	public MyThreadPoolExecutor getExecutor() {
		return this.executor;
	}

	public int getQueueSize() {
		return this.workQueue.size();// 等待执行任务数
	}

	public int getThreadNumber() {
		return this.threadFactory.getThreadCount();// 当前线程数
	}

	public int getActiveThread() {
		return this.executor.getActiveCount();// 活动线程数
	}

	public List<MyThreadStack> getAllThreadStacks() {
		return this.threadFactory.getAllThreadStacks();// 线程堆栈信息
	}

	public boolean isPaused() {
		return this.executor.isPaused();
	}

	public void pause() {
		if (!this.executor.isPaused()) {
			this.executor.pause();
			handler.setRejectedErrMsg(DEFAULT_PAUSED_ERRMSG);
		}
	}

	public void resume() {
		if (this.executor.isPaused()) {
			this.executor.resume();
			handler.setRejectedErrMsg(DEFAULT_REJECTED_ERRMSG);
		}
	}

	public Future<?> submit(Runnable r) {
		return this.executor.submit(r);
	}

	public Future<?> submit(Callable<?> r) {
		return this.executor.submit(r);
	}


}
